package com.xinqi.modules.prepare.folder.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.dto.CommonFileDTO;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.utils.lambda.Lambdas;
import com.xinqi.common.mybatis.data.entity.SuperEntity;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.common.domain.PrepareBaseParam;
import com.xinqi.modules.prepare.common.enums.PrepareLimitEnum;
import com.xinqi.modules.prepare.common.utils.CommonFileUtils;
import com.xinqi.modules.prepare.common.utils.PrepareTypeUtils;
import com.xinqi.modules.prepare.courseware.manager.PrepareCoursewareManager;
import com.xinqi.modules.prepare.data.manager.PrepareDataManager;
import com.xinqi.modules.prepare.discuss.manager.PrepareDiscussManager;
import com.xinqi.modules.prepare.folder.convert.PrepareFileFolderConvert;
import com.xinqi.modules.prepare.folder.domain.PrepareFileFolderEntity;
import com.xinqi.modules.prepare.folder.dto.req.PrepareFileFolderQueryDTO;
import com.xinqi.modules.prepare.folder.dto.rsp.PrepareFileFolderResultDTO;
import com.xinqi.modules.prepare.folder.manager.PrepareFileFolderManager;
import com.xinqi.modules.prepare.folder.mapper.PrepareFileFolderMapper;
import com.xinqi.modules.prepare.homework.manager.PrepareHomeworkManager;
import com.xinqi.modules.prepare.notice.manager.PrepareNoticeManager;
import com.xinqi.modules.prepare.paper.manager.PrepareExamPaperManager;
import com.xinqi.modules.prepare.prepare.enums.PrepareFileTypeEnum;
import com.xinqi.modules.prepare.prepare.enums.PrepareTypeEnum;
import com.xinqi.modules.prepare.question.manager.PrepareQuestionBankManager;
import com.xinqi.modules.prepare.video.manager.PrepareVideoManager;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.xinqi.common.base.error.CommonErrors.BAD_REQUEST;

/**
 * 备课区文件夹管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareFileFolderManagerImpl extends BaseServiceImpl<PrepareFileFolderMapper, PrepareFileFolderEntity> implements PrepareFileFolderManager {
    private final PrepareCoursewareManager prepareCoursewareManager;
    private final PrepareVideoManager prepareVideoManager;
    private final PrepareDataManager prepareDataManager;
    private final PrepareHomeworkManager prepareHomeworkManager;
    private final PrepareDiscussManager prepareDiscussManager;
    private final PrepareNoticeManager prepareNoticeManager;
    private final PrepareExamPaperManager prepareExamPaperManager;
    private final PrepareFileFolderConvert prepareFileFolderConvert;
    private final PrepareQuestionBankManager prepareQuestionBankManager;

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareFileFolderResultDTO> findPage(Page<Object> page, PrepareFileFolderQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareFileFolderResultDTO> findList(PrepareFileFolderQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区文件夹数据
     */
    @Override
    public PrepareFileFolderEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id查询 被删除的 备课区文件夹数据
     */
    @Override
    public PrepareFileFolderEntity findDeletedById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.baseMapper.findDeletedById(id);
    }


    /**
     * 根据id删除 备课区文件夹数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        PrepareFileFolderEntity entity = Optional.ofNullable(getById(id)).orElseThrow(() -> BAD_REQUEST.asException("数据不存在"));
        return delete(entity);
    }

    /**
     * 根据id新增 备课区文件夹数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrepareFileFolderEntity insertFile(PrepareBaseParam obj, Long prepareId, Long folderPid) {
        return insertFile(obj, prepareId, folderPid, false);
    }

    /**
     * 根据id新增 备课区文件夹数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrepareFileFolderEntity insertFile(PrepareBaseParam obj, Long prepareId, Long folderPid, Boolean isAutoGenerateName) {
        BAD_REQUEST.check(prepareId != null, "备课区不能为空");
        PrepareFileFolderEntity entity = new PrepareFileFolderEntity();
        entity.setRefId(obj.getId());
        entity.setPrepareId(prepareId);
        entity.setFileType(PrepareFileTypeEnum.FILE);
        entity.setPid(folderPid == null ? 0 : folderPid);
        // 获取父目录
        PrepareFileFolderEntity parentFolder = getParentFolder(prepareId, folderPid);
        setPrepareFile(obj, entity, parentFolder, isAutoGenerateName);
        save(entity);
        return entity;
    }

    /**
     * 根据id修改 备课区文件夹数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrepareFileFolderEntity updateFile(PrepareBaseParam obj, Long prepareId) {
        BAD_REQUEST.check(prepareId != null, "备课区不能为空");
        PrepareFileFolderEntity entity = Optional.ofNullable(findByRefId(obj.getId())).orElseThrow(() -> BAD_REQUEST.asException("备课区文件不存在"));
        BAD_REQUEST.check(Objects.equals(entity.getFileType(), PrepareFileTypeEnum.FILE), "备课区文件类型错误");
        PrepareFileFolderEntity parentFolder = getParentFolder(prepareId, entity.getPid());
        setPrepareFile(obj, entity, parentFolder, false);
        updateById(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrepareFileFolderEntity deleteFile(PrepareBaseParam obj, Long prepareId) {
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareFileFolderEntity::getPrepareId, prepareId);
        wrapper.eq(PrepareFileFolderEntity::getRefId, obj.getId());
        PrepareFileFolderEntity entity = getOne(wrapper);
        delete(entity);
        return entity;
    }

    /**
     * 设置备课区文件信息
     *
     * @param entity
     * @param parentFolder
     */
    private void setPrepareFile(PrepareBaseParam obj, PrepareFileFolderEntity entity, PrepareFileFolderEntity parentFolder, Boolean isAutoGenerateName) {
        checkPrepareType(parentFolder, obj.getPrePareType());
        entity.setType(PrepareTypeUtils.get(obj));
        BAD_REQUEST.check(Objects.equals(obj.getPrepareId(), entity.getPrepareId()), "备课区不匹配");
        initFileData(entity, parentFolder, obj.getName(), obj.getFiles(), isAutoGenerateName);
    }

    /**
     * 获取父文件夹
     */
    private PrepareFileFolderEntity getParentFolder(Long prepareId, Long folderPid) {
        PrepareFileFolderEntity parentFolder = null;
        if (folderPid != null && folderPid != 0L) {
            parentFolder = Optional.ofNullable(findById(folderPid)).orElseThrow(() -> BAD_REQUEST.asException("父文件夹不存在"));
            BAD_REQUEST.check(Objects.equals(parentFolder.getFileType(), PrepareFileTypeEnum.FOLDER), "父文件夹不能为文件");
            BAD_REQUEST.check(Objects.equals(parentFolder.getPrepareId(), prepareId), "备课区id不一致");
        }
        return parentFolder;
    }

    /**
     * 根据引用ID查询备课区文件
     */
    public PrepareFileFolderEntity findByRefId(Long refId) {
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareFileFolderEntity::getRefId, refId);
        return getOne(wrapper);
    }

    /**
     * 校验备课区类型
     */
    private void checkPrepareType(PrepareFileFolderEntity folder, PrepareTypeEnum type) {
        if (folder == null) {
            return;
        }
        BAD_REQUEST.check(Objects.equals(folder.getType(), type), "备课区类型不匹配");
    }

    /**
     * 初始化文件数据
     */
    private void initFileData(PrepareFileFolderEntity entity, PrepareFileFolderEntity parentFolder,
                              String name, String files, Boolean isAutoGenerateName) {
        // 设置备课区文件名称
        entity.setName(name);
        // 判断备课区文件重复
        if (isExistName(entity.getPrepareId(), entity.getType(), entity.getName(), entity.getPid(), entity.getFileType(), entity.getId())) {
            if (Objects.equals(isAutoGenerateName, Boolean.TRUE)) {
                Long maxNum = getMaxNum(entity.getPrepareId(), entity.getType(), entity.getName(), entity.getPid(), entity.getFileType());
                name = name + "_" + maxNum;
                entity.setName(name);
            } else {
                throw BAD_REQUEST.asException("文件名称重复");
            }
        }
        // 设置备课区文件的文件路劲
        entity.setFilePath(CommonFileUtils.filePath(name, parentFolder));
        entity.setLevel(CommonFileUtils.fileLevel(entity.getFilePath()));
        // 设置备课区文件的文件列表
        entity.setFiles(files);
        // 设置备课区文件的文件总大小
        List<CommonFileDTO> fileList = CommonFileUtils.toFileObj(files);
        PrepareLimitEnum.文件列表数量限制.check(fileList.size());
        Long filesSize = CommonFileUtils.fileSize(fileList);
        PrepareLimitEnum.文件列表大小限制.check(filesSize);
        entity.setSize(filesSize);
    }

    /**
     * 根据id删除 备课区文件夹数据
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(PrepareFileFolderEntity entity) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(entity), "删除数据不能为空");
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = pathAllWrapper(entity);
        // 获取文件列表
        List<PrepareFileFolderEntity> list = list(wrapper);
        // 删除关联数据
        deleteFileRef(entity.getType(), list.stream().filter(v -> Objects.equals(v.getFileType(), PrepareFileTypeEnum.FILE)).map(PrepareFileFolderEntity::getRefId).collect(Collectors.toList()));
        return this.remove(wrapper);
    }

    /**
     * 查询当前备课区文件路径下的所有对象（包括自己）
     */
    private LambdaQueryWrapper<PrepareFileFolderEntity> pathAllWrapper(PrepareFileFolderEntity entity) {
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareFileFolderEntity::getPrepareId, entity.getPrepareId());
        wrapper.eq(PrepareFileFolderEntity::getType, entity.getType().getCode());
        wrapper.likeRight(PrepareFileFolderEntity::getFilePath, entity.getFilePath());
        wrapper.eq(PrepareFileFolderEntity::getIsDelete, YesNoEnum.NO.ordinal());
        return wrapper;
    }

    @Override
    public Boolean isExistName(Long prepareId, PrepareTypeEnum type, String name, Long pid, PrepareFileTypeEnum fileType, Long oId) {
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareFileFolderEntity::getPrepareId, prepareId);
        wrapper.eq(PrepareFileFolderEntity::getType, type.getCode());
        wrapper.eq(PrepareFileFolderEntity::getName, name);
        wrapper.eq(PrepareFileFolderEntity::getPid, pid);
        wrapper.eq(PrepareFileFolderEntity::getFileType, fileType.getCode());
        wrapper.eq(PrepareFileFolderEntity::getIsDelete, YesNoEnum.NO.ordinal());
        wrapper.ne(oId != null, PrepareFileFolderEntity::getId, oId);
        return count(wrapper) > 0;
    }

    @Override
    public Long getMaxNum(Long prepareId, PrepareTypeEnum type, String name, Long pid, PrepareFileTypeEnum fileType) {
        return this.baseMapper.getMaxNum(prepareId, type.getCode(), name, pid, fileType.getCode());
    }

    @Override
    public List<PrepareFileFolderEntity> findByPid(Long pid) {
        LambdaQueryWrapper<PrepareFileFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareFileFolderEntity::getPid, pid);
        wrapper.eq(PrepareFileFolderEntity::getIsDelete, YesNoEnum.NO.ordinal());
        return list(wrapper);
    }

    @Override
    public void setChildrenPath(List<PrepareFileFolderEntity> list, Long pid, String path) {
        List<PrepareFileFolderEntity> children = findByPid(pid);
        children.forEach(v -> {
            PrepareFileFolderEntity entity = new PrepareFileFolderEntity();
            entity.setId(v.getId());
            entity.setFilePath(CommonFileUtils.filePath(v.getName(), path));
            entity.setLevel(CommonFileUtils.fileLevel(entity.getFilePath()));
            setChildrenPath(list, v.getId(), entity.getFilePath());
            list.add(entity);
        });
    }

    private void checkMoveOrCopyLevel(PrepareFileFolderEntity source, PrepareFileFolderEntity parentFolder) {
        // 校验文件层次深度
        if (Objects.equals(source.getFileType(), PrepareFileTypeEnum.FOLDER) && parentFolder != null) {
            int maxLevel = this.baseMapper.maxLevel(source.getPrepareId(), PrepareFileTypeEnum.FOLDER.getCode(), source.getFilePath());
            PrepareLimitEnum.文件夹深度.check(maxLevel - source.getLevel() + 1 + parentFolder.getLevel());
        }
    }

    /**
     * 移动到
     */
    @Override
    public void move(PrepareFileFolderEntity source, Long targetPrepareId, Long targetPid) {
        PrepareFileFolderEntity parentFolder = null;
        if (targetPid != 0) {
            parentFolder = Lambdas.ofNull(findById(targetPid), "目标父目录不存在");
        }
        checkMoveOrCopyLevel(source, parentFolder);
        if (parentFolder == null) {
            move(source, targetPrepareId);
        } else {
            move(source, parentFolder);
        }
        updateById(source);
    }

    /**
     * 移动文件、文件 到 备课区下
     */
    private void move(PrepareFileFolderEntity source, Long targetPrepareId) {
        source.setPid(0L);
        source.setPrepareId(targetPrepareId);
        // 设置名称和路径
        setNameAndPath(source, null);
        move(source);
    }

    /**
     * 移动文件、文件 到 父目录下
     */
    private void move(PrepareFileFolderEntity source, PrepareFileFolderEntity parentFolder) {
        source.setPid(parentFolder.getId());
        source.setPrepareId(parentFolder.getPrepareId());
        // 设置名称和路径
        setNameAndPath(source, parentFolder.getFilePath());
        // 如果是文件夹，则移动该目录下的所有子文件和目录
        move(source);
    }

    /**
     * 移动文件
     */
    private void move(PrepareFileFolderEntity source) {
        switch (source.getFileType()) {
            case FOLDER:
                List<PrepareFileFolderEntity> childList = findByPid(source.getId());
                childList.forEach(v -> move(v, source));
                updateBatchById(childList);
                break;
            case FILE:
                moveFileRef(source);
                break;
        }
    }

    /**
     * 移动文件引用数据
     */
    private void moveFileRef(PrepareFileFolderEntity file) {
        PrepareTypeEnum type = file.getType();
        switch (type) {
            case DATA:
                // 资料
                prepareDataManager.move(file);
                break;
            case DISCUSS:
                // 讨论
                prepareDiscussManager.move(file);
                break;
            case NOTICE:
                // 公告
                prepareNoticeManager.move(file);
                break;
            case HOMEWORK:
                // 作业
                prepareHomeworkManager.move(file);
                break;
            case PAPER:
                // 测评
                prepareExamPaperManager.move(file);
                break;
            case INTERACT_COURSEWARE:
                // 互动课件
                prepareCoursewareManager.move(file);
                break;
            case INTERACT_VIDEO:
                // 互动视频
                prepareVideoManager.move(file);
                break;
            default:
                throw BAD_REQUEST.asException("为实现复制功能");
        }
    }

    /**
     * 复制到
     */
    @Override
    public void copy(PrepareFileFolderEntity source, Long targetPrepareId, Long targetPid) {
        PrepareFileFolderEntity parentFolder = null;
        if (targetPid != 0) {
            parentFolder = Lambdas.ofNull(findById(targetPid), "目标父目录不存在");
        }
        checkMoveOrCopyLevel(source, parentFolder);
        if (parentFolder == null) {
            copy(source, targetPrepareId);
        } else {
            copy(source, parentFolder);
        }
    }

    /**
     * 将文件、文件夹 复制到 备课区下
     */
    private void copy(PrepareFileFolderEntity source, Long prepareId) {
        // 复制 文件或者文件夹
        PrepareFileFolderEntity target = prepareFileFolderConvert.copy(source, new PrepareFileFolderEntity());
        target.setId(null);
        // 设置 根节点
        target.setPid(0L);
        target.setPrepareId(prepareId);
        // 设置名称和路径
        setNameAndPath(target, null);
        // 处理
        processCopy(source, target);
    }

    /**
     * 将文件、文件夹 复制到 对应目录下
     */
    private void copy(PrepareFileFolderEntity source, PrepareFileFolderEntity parentTarget) {
        // 复制 文件或者文件夹
        PrepareFileFolderEntity target = prepareFileFolderConvert.copy(source, new PrepareFileFolderEntity());
        target.setId(null);
        target.setPid(parentTarget.getId());
        target.setPrepareId(parentTarget.getPrepareId());
        // 生成不重名的副本名称
        setNameAndPath(target, parentTarget.getFilePath());
        // 处理
        processCopy(source, target);
    }

    /**
     * 处理复制
     */
    private void processCopy(PrepareFileFolderEntity source, PrepareFileFolderEntity parentTarget) {
        // 判断文件类型
        switch (source.getFileType()) {
            case FOLDER:
                // 这里保存是是为了获取 id
                save(parentTarget);
                // 是目录，需要复制源文件目录下的所有文件
                findByPid(source.getId()).forEach(v -> copy(v, parentTarget));
                break;
            case FILE:
                // 是文件，需要复制文件关联业务数据
                copyFileRef(source, parentTarget);
                // 这里保存是未了获取 refId
                save(parentTarget);
                break;
        }
    }

    /**
     * 复制引用数据
     */
    private void copyFileRef(PrepareFileFolderEntity source, PrepareFileFolderEntity target) {
        PrepareTypeEnum type = source.getType();
        switch (type) {
            case DATA:
                // 资料
                prepareDataManager.copy(source.getRefId(), target);
                break;
            case DISCUSS:
                // 讨论
                prepareDiscussManager.copy(source.getRefId(), target);
                break;
            case NOTICE:
                // 公告
                prepareNoticeManager.copy(source.getRefId(), target);
                break;
            case HOMEWORK:
                // 作业
                prepareHomeworkManager.copy(source.getRefId(), target);
                break;
            case QUESTION_BANK:
                //题库
                prepareQuestionBankManager.copy(source.getRefId(), target);
                break;
            case PAPER:
                // 测评
                prepareExamPaperManager.copy(source.getRefId(), target);
                break;
            case INTERACT_COURSEWARE:
                // 互动课件
                prepareCoursewareManager.copy(source.getRefId(), target);
                break;
            case INTERACT_VIDEO:
                // 互动视频
                prepareVideoManager.copy(source.getRefId(), target);
                break;
            default:
                throw BAD_REQUEST.asException("为实现复制功能");
        }
    }

    /**
     * 设置文件名称和路径
     */
    private void setNameAndPath(PrepareFileFolderEntity entity, String parentPath) {
        String newName = autoCreateName(entity.getName(), entity.getPrepareId(), entity.getType(), entity.getFileType(), entity.getPid());
        entity.setName(newName);
        entity.setFilePath(CommonFileUtils.filePath(newName, parentPath));
        entity.setLevel(CommonFileUtils.fileLevel(entity.getFilePath()));
    }

    /**
     * 自动生成文件名（不重复）
     */
    private String autoCreateName(String name, Long prepareId, PrepareTypeEnum type, PrepareFileTypeEnum fileType, Long pid) {
        String newName = name;
        int count = 0;
        while (count <= 5) {
            count++;
            if (isExistName(prepareId, type, newName, pid, fileType, null)) {
                newName = name + "副本(" + count + ")";
            } else {
                return newName;
            }
        }
        throw BAD_REQUEST.asException("副本数量过多");
    }

    /**
     * 还原文件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void restore(Long refId, Long prepareId, PrepareTypeEnum type) {
        // 查询被删除文件
        PrepareFileFolderEntity entity = Lambdas.ofNull(this.baseMapper.findDeletedByRefId(refId), "备课区文件不存在");
        // 如果父文件夹不存在，则还原到跟目录中
        if (!baseMapper.exists(new LambdaQueryWrapper<PrepareFileFolderEntity>().eq(PrepareFileFolderEntity::getId, entity.getPid()))) {
            entity.setPid(0L);
        }
        // 还原文件
        this.baseMapper.restoreById(entity.getId());
        // 恢复引用数据
        restoreRef(entity);
    }

    /**
     * 还原应用对象
     */
    private void restoreRef(PrepareFileFolderEntity entity) {
        PrepareTypeEnum type = entity.getType();
        Long refId = entity.getRefId();
        switch (type) {
            case DATA:
                // 资料
                prepareDataManager.restore(refId);
                break;
            case DISCUSS:
                // 讨论
                prepareDiscussManager.restore(refId);
                break;
            case NOTICE:
                // 公告
                prepareNoticeManager.restore(refId);
                break;
            case HOMEWORK:
                // 作业
                prepareHomeworkManager.restore(refId);
                break;
            case PAPER:
                // 测评
                prepareExamPaperManager.restore(refId);
                break;
            case INTERACT_COURSEWARE:
                // 互动课件
                prepareCoursewareManager.restore(refId);
                break;
            case INTERACT_VIDEO:
                // 互动视频
                prepareVideoManager.restore(refId);
                break;
            default:
                throw BAD_REQUEST.asException("为实现复制功能");
        }
    }

    /**
     * 删除权限子文件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllChild(PrepareFileFolderEntity entity) {
        List<PrepareFileFolderEntity> delList = new ArrayList<>();
        List<Long> refIdList = new ArrayList<>();
        // 获取需要删除对象
        getDeleteObj(delList, refIdList, entity);
        // 删除文件目录
        this.delete(delList.stream().map(SuperEntity::getId).collect(Collectors.toList()));
        // 删除引用数据
        this.deleteFileRef(entity.getType(), refIdList);
    }

    @Override
    public List<PrepareFileFolderEntity> findByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 获取需要删除的对象
     */
    private void getDeleteObj(List<PrepareFileFolderEntity> delList, List<Long> refIdList, PrepareFileFolderEntity entity) {
        if (Objects.equals(entity.getFileType(), PrepareFileTypeEnum.FOLDER)) {
            // 删除文件夹下的文件
            findByPid(entity.getId()).forEach(this::deleteAllChild);
        } else {
            // 删除文件引用
            refIdList.add(entity.getRefId());
        }
        delList.add(entity);
    }

    /**
     * 删除文件应用对象
     */
    private void deleteFileRef(PrepareTypeEnum type, List<Long> refIdList) {
        if (CollectionUtils.isEmpty(refIdList)) {
            return;
        }
        switch (type) {
            case DATA:
                // 资料
                refIdList.forEach(prepareDataManager::deleteById);
                break;
            case DISCUSS:
                // 讨论
                refIdList.forEach(prepareDiscussManager::deleteById);
                break;
            case NOTICE:
                // 公告
                refIdList.forEach(prepareNoticeManager::deleteById);
                break;
            case HOMEWORK:
                // 作业
                refIdList.forEach(prepareHomeworkManager::deleteById);
                break;
            case PAPER:
                // 测评
                refIdList.forEach(prepareExamPaperManager::deleteById);
                break;
            case INTERACT_COURSEWARE:
                // 互动课件
                refIdList.forEach(prepareCoursewareManager::deleteById);
                break;
            case INTERACT_VIDEO:
                // 互动视频
                refIdList.forEach(prepareVideoManager::deleteById);
                break;
            default:
                throw BAD_REQUEST.asException("为实现复制功能");
        }
    }

    @Override
    public long fileSizeTotal(Long prepareId) {
        BAD_REQUEST.check(prepareId != null, "备课ID不能为空");
        return this.baseMapper.fileSizeTotal(prepareId);
    }

}
